<template>
  <el-dialog title="手机卡信息添加" :visible.sync="dialog_visible">
    <div class="form-padding">
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="temp"
        label-position="right"
        auto-complete="on"
        label-width="90px"
        style="width: 100%"
      >
        <el-form-item label="序号" prop="id">
          <el-input
            disabled
            style="width: 100%"
            placeholder="新增无需序号"
            v-model="temp.id"
          />
        </el-form-item>
        <el-form-item label="手机号" prop="phone_num">
          <el-input
            placeholder="请输入手机号"
            style="width: 100%"
            v-model="temp.phone_num"
          />
        </el-form-item>
        <el-form-item label="实名人" prop="belong_to">
          <el-input
            placeholder="请输入实名人"
            style="width: 100%"
            v-model="temp.belong_to"
          />
        </el-form-item>
        <el-form-item label="月套餐" prop="tel_package">
          <tinymce
            :height="200"
            :toolbar="toolBar"
            :fresh.sync="fresh" 
            v-model="temp.tel_package"
          />
        </el-form-item>
        <el-form-item label="流量" prop="tel_traffic">
          <el-input
            placeholder="请输入流量"
            style="width: 100%"
            v-model="temp.tel_traffic"
          />
        </el-form-item>
        <el-form-item label="卡类型" prop="tel_type">
          <el-select
            v-model="temp.tel_type"
            style="width: 100%"
            placeholder="请输入卡类型"
          >
            <el-option
              v-for="{ id, name } in typeArray"
              :key="'typeArray' + id"
              :label="name"
              :value="name"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="流量优惠到期日" prop="discount_exp_date">
          <el-date-picker
            style="width: 100%"
            v-model="temp.discount_exp_date"
            type="date"
            placeholder="请输入"
          />
        </el-form-item>
        <el-form-item label="微信密码" prop="wechat_pwd">
          <el-input
            placeholder="请输入微信密码"
            style="width: 100%"
            v-model="temp.wechat_pwd"
          />
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取消</el-button>
      <el-button type="primary" @click="submit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { notExistAdd, typeArray } from "@/api/adm_cart";
import g from "@/utils/utilFn";
import Tinymce from "@/components/Tinymce/tinymce";

export default {
  name: "cartAdd",
  components: { Tinymce },
  props: {
    show: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    const validaterole_name = (rule, value, callback) => {
      if (value.length > 30) {
        callback(new Error("名称不能太长！"));
      } else {
        callback();
      }
    };
    return {
      fresh:false,
      typeArray: [],
      toolBar: [
        "searchreplace bold italic underline strikethrough alignleft aligncenter alignright",
        "hr bullist numlist link  emoticons forecolor fullscreen",
      ],
      temp: {
        id: "",
        phone_num: "",
        belong_to: "",
        tel_package: "",
        tel_traffic: "",
        tel_type: "主卡",
        discount_exp_date: "",
        wechat_pwd: "",
      },
      rules: {
        phone_num: [
          { required: true, message: "手机号码不能为空", trigger: "blur" },
          { required: true, trigger: "blur", validator: validaterole_name },
        ],
        belong_to: [
          { required: true, message: "实名人不能为空", trigger: "blur" },
          { required: true, trigger: "blur", validator: validaterole_name },
        ],
        tel_package: [
          { required: true, message: "月套餐不能为空", trigger: "blur" },
        ]
      },
    };
  },
  computed: {
    dialog_visible: {
      get() {
        return this.show;
      },
      set(val) {
        this.$emit("update:show", val);
      },
    },
  },
  created() {
    this.typeArray = typeArray;
  },
  watch: {
    dialog_visible(newValue, oldValue) {
      if (newValue) {
       this.fresh = true
      }
    },
  },
  methods: {
    cancel() {
      this.dialog_visible = false;
    },
    submit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          const param = {
            phone_num: this.temp.phone_num,
            belong_to: this.temp.belong_to,
            tel_package: this.temp.tel_package,
            tel_traffic: this.temp.tel_traffic,
            tel_type: this.temp.tel_type,
            discount_exp_date: g.format(
              this.temp.discount_exp_date,
              "yyyy-MM-dd"
            ),
            wechat_pwd: this.temp.wechat_pwd,
          };
          notExistAdd(param).then((response) => {
            if (response.data > 0) {
              setTimeout(() => {
                this.dialog_visible = false;
                this.$message({
                  type: "success",
                  message: "手机卡信息添加成功!",
                });
                this.$refs["dataForm"].resetFields();
                this.$emit("success", param);
              }, 200);
            } else {
              this.$message({
                type: "info",
                message: "手机卡信息添加失败,有没有电话号码重复",
              });
            }
          });
        }
      });
    },
  },
};
</script>